package com.pz.chat.mq;

import com.pz.chat.constant.MessageTypeEnum;
import com.pz.chat.constant.RabbitMqConstant;
import com.pz.chat.entity.ChatMessageEntity;
import com.pz.chat.service.ChatMessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class ChatMessageListener {
    @Autowired
    private ChatMessageService chatMessageService;

    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(name = RabbitMqConstant.chatMsgExchange),
            value = @Queue(name = RabbitMqConstant.chatMsgQue),
            key = {RabbitMqConstant.chatMsgRoutingKey}
    ))
    private void handleChatMessage(ChatMessageEntity chatMessage) {
        if (chatMessage.getType().intValue() == MessageTypeEnum.CLIENT_ACK_MSG.getType()) {
            // 确定收取消息
            chatMessage.setStatus(1);
            chatMessageService.updateById(chatMessage);
        } else {
            // 存储新消息
            chatMessage.setMsgId(null);
            chatMessageService.save(chatMessage);
        }
    }
}
